import { _HttpClient } from '@delon/theme';
import { Injectable } from '@angular/core';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { CourseDTO } from 'src/app/project/model/icm/icm-base/course/course.dto';
import { ApiSimpleData } from 'src/app/project/model/common/api-simple-data';

@Injectable({
  providedIn: 'root',
})
export class CourseService {
  private static URL = '/api/icm-course';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询课程信息
   * @param option 分页参数
   * @param code 课程编号
   * @param name 课程名称
   * @param courseTypeId 课程类型id
   * @param evaluationMode 考核方式
   */
  findOnePage(
    option: QueryOptions,
    code?: string,
    name?: string,
    courseTypeId?: string,
    evaluationMode?: string,
  ): Observable<ApiPagedData<CourseDTO>> {
    const params = {};
    Object.assign(
      params,
      option,
      code ? { code } : {},
      name ? { name } : {},
      courseTypeId ? { courseTypeId } : {},
      evaluationMode ? { evaluationMode } : {},
    );
    return this.http.get<ApiPagedData<CourseDTO>>(CourseService.URL, params);
  }

  /**
   * 添加课程
   * @param courseDTO 课程信息
   */
  create(courseDTO: CourseDTO): Observable<CourseDTO> {
    return this.http.post<CourseDTO>(CourseService.URL, courseDTO);
  }

  /**
   * 根据id查找课程
   * @param id 课程id
   */
  findById(id: string): Observable<CourseDTO> {
    return this.http.get<CourseDTO>(`${CourseService.URL}/${id}`);
  }

  /**
   * 修改更新课程信息
   * @param id 课程id
   * @param courseDTO 课程信息
   */
  complete(id: string, courseDTO: CourseDTO): Observable<CourseDTO> {
    return this.http.put<CourseDTO>(`${CourseService.URL}/${id}`, courseDTO);
  }

  /**
   * 根据id删除课程
   * @param id 课程id
   */
  delete(id: string): Observable<CourseDTO> {
    return this.http.delete<CourseDTO>(`${CourseService.URL}/${id}`);
  }

  /**
   * 查找所有课程，可按需求排序
   * @param param 排序参数
   * @param pattern 排序模式: asc/desc
   */
  findAllBySort(param?: string, pattern?: string): Observable<Array<CourseDTO>> {
    const params = {};
    Object.assign(params, param ? { param } : {}, pattern ? { pattern } : {});
    return this.http.get<Array<CourseDTO>>(`${CourseService.URL}/all`, params);
  }

  /**
   * 根据课程code，检查课程code是否已存在
   * @param code 课程code
   */
  checkoutCodeIsExist(code: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, code ? { code } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${CourseService.URL}/checkCodeExist`, params);
  }
}
